<?php

namespace app\agent\controller;

use think\Validate;
use think\Db;
use app\models\{Banner, Cate, Config, Annous, Agent as Agents, Subcate};

class System extends Base
{


    public function subcate()
    {
        if ($this->request->isAjax()) {
            $model = new Subcate();
            $post = input();
            $page = input('page/d') ?? 1;
            $limit = input('limit/d') ?? 10;
            $where = array();
            $where[] = ['agentid', '=', 0];
            $where[] = ['isshow', '=', 1];

            if (!empty($post['title'])) {
                $where[] = ['name', 'like', '%' . $post['title'] . '%'];
            }
            if (!empty($post['cate'])) {
                $where[] = ['cate', '=', $post['cate']];
            }
            $count = $model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $model->where($where)->order(["sort" => "desc", "id" => 'desc'])->limit($limit)->page($page)->select();
            foreach ($list as $lists) {
                $lists['daoru'] = 2;
                $lists['cate_name'] = (new Cate())->where(['id' => $lists['cate']])->value('name') ?? '无';
                $have = $model->where(['agentid' => $this->request->agent['id'], 'topid' => $lists['id']])->find();
                if (!empty($have)) {
                    $lists['daoru'] = 1;
                }
            }
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
        } else {
            return view(null, [
                'cate' => (new Cate())->field('id,name,isshow')->order(['sort' => 'desc'])->select()
            ]);
        }
    }

    public function banner()
    {
        if ($this->request->isAjax()) {
            $model = new Banner();
            $post = input();
            $page = input('page/d') ?? 1;
            $limit = input('limit/d') ?? 10;
            $where = array();
            $where[] = ['agentid', '=', $this->request->agent['id']];
            $count = $model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $model->where($where)->order("id desc")->limit($limit)->page($page)->select();
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
        } else {
            return view(null, []);
        }
    }

    public function banner_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'url' => 'require'
            ], [
                'title.require' => '轮播图备注信息不能为空',
                'url.require' => '图片路径不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            $data['types'] = 1;
            $data['agentid'] = $this->request->agent['id'];
            $model = new Banner();
            //查找店铺名称、账号是否已经被使用
            if (!empty($data['id'])) {
                $res = $model->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = $model->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['code' => 200, 'data' => [], 'msg' => '操作成功']);
            }
            return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
        } else {
            $model = new Banner();
            $id = $this->request->get('id/d', 0);
            $row = $model->where('id', $id)->find();
            return view('', [
                'row' => $row,
                'agentid' => $this->request->agent['id']
            ]);
        }
    }

    public function banner_del()
    {
        $id = input('id');
        if (!empty($id)) {
            $model = new Banner();
            Db::startTrans();
            try {
                $model->where(['id' => $id, 'agentid' => $this->request->agent['id']])->delete();
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return json(['code' => 1, 'data' => [], 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败']);
    }


    public function annous()
    {
        if ($this->request->isAjax()) {
            $model = new Annous();
            $post = input();
            $page = input('page/d') ?? 1;
            $limit = input('limit/d') ?? 10;
            $where = array();
            if (!empty($post['types'])) {
                $where[] = ['types', '=', $post['types']];
            }
            $where['agentid'] = $this->request->agent['id'];
            $count = $model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $model->where($where)->order("id desc")->limit($limit)->page($page)->select();
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
        } else {
            return view(null, []);
        }
    }

    public function annous_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'types' => 'require|in:1,2',
                'content' => 'require'
            ], [
                'title.require' => '轮播图备注信息不能为空',
                'types.require' => '类别选择不能为空',
                'types.in' => '类别不正确',
                'content.require' => '内容不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            $data['agentid'] = $this->request->agent['id'];
            $model = new Annous();
            //查找店铺名称、账号是否已经被使用
            if (!empty($data['id'])) {
                $res = $model->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = $model->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['code' => 200, 'data' => [], 'msg' => '操作成功']);
            }
            return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
        } else {
            $model = new Annous();
            $id = $this->request->get('id/d', 0);
            $row = $model->where('id', $id)->find();
            return view('', [
                'row' => $row
            ]);
        }
    }

    public function annous_del()
    {
        $id = input('id');
        if (!empty($id)) {
            $model = new Annous();
            Db::startTrans();
            try {
                $model->where(['id' => $id, 'agentid' => $this->request->agent['id']])->delete();
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return json(['code' => 1, 'data' => [], 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败']);
    }
}
